﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginarium.team;
using Imaginarium.battleSistem;
using Microsoft.Xna.Framework.Graphics;
using GuiLibrary.layouts;
using GameEngine.Scenes;
using GuiLibrary.Components;
using GameEngine.animations;
using Microsoft.Xna.Framework;
using GameEngine.utils;
using GuiLibrary;

namespace Imaginarium.gui
{
    public class SelectSkillGui: Gui, IOnCancel, IOnElementPointed
    {

        public Character performerCharacter { get; set; }

        #region Constructors
        public SelectSkillGui(Character performer) :
            base()
        {
            this.performerCharacter = performer;
        }
        #endregion

        public override void LoadContent()
        {
            #region ObtainRequiredData
            //background = CodeParameters.backgroundSkillGUI();
            //background.position = this.position;
            //sceneResolution = background.getCurrentAnimation().getFrameSize();
            List<Element> optionElements = new List<Element>();
            if (performerCharacter is Player)
            {
                Player player = (Player)performerCharacter;
                foreach (SkillDetail sd in player.currentJob.skillDetail)
                    //optionElements.Add(new Element(bo.name, new BattleOption(bo.type,bo.name)));
                    optionElements.Add(new ImaginariumElement(sd));
            }
            else if (performerCharacter is Enemy)
            {
                Enemy enemy = (Enemy)performerCharacter;
                foreach (SkillDetail sd in enemy.skillDetails)
                    optionElements.Add(new ImaginariumElement(sd));
            }
            SpriteFont font = Imaginarium.ImaginariumGame.getInstance().Content.Load<SpriteFont>(CodeParameters.selectSkillFont);
            #endregion

            #region CreateGuiComponents
            basePanel = new Panel(position, GUIParameters.backgroundSkillGUI());
            Panel skillSelPanel = new Panel(basePanel);
            Panel skillDescPanel = new Panel(basePanel);
            basePanel.components.Add(skillSelPanel);
            basePanel.components.Add(skillDescPanel);
            foreach (Element option in optionElements) 
            {
                bool canCast = performerCharacter.canCast((SkillDetail)option.associatedObject);
                SelectableLabel selLbl = new SelectableLabel(skillSelPanel, new Vector2(), new Text(font, option.name, canCast ? Color.White : new Color(Color.DarkGray,0.2f), OriginType.LEFT_UP), option);
                skillSelPanel.components.Add(selLbl);
            }
            basePanel.layout = new ListLayout(basePanel, GUIParameters.marginsZERO, GUIParameters.weightsSkillSelectGUI, false, null, null, null);
            //basePanel.layout.refresh();
            skillSelPanel.layout = new GridLayout(skillSelPanel, GUIParameters.marginsSkillSelectGUI, 4, 4, this, this, this);
            //skillSelPanel.layout.refresh();
            skillDescPanel.layout = new ListLayout(skillDescPanel, GUIParameters.marginsSkillDescriptionGUI, GUIParameters.weightsSkillDescriptionGUI, true, null, null, null);
            basePanel.refresh();
            if(optionElements.Count>0)onElementPointed(optionElements[0]); //this method displays the skill description and does a refresh
            #endregion
        }

        public override void onSelectedElement(Element e)
        {
            Message msg = BattleManager.getInstance().turnExpert.setAbilityCommand((SkillDetail)e.associatedObject);
            if (msg.success)
                SceneManager.getInstance().scenes.Add(createTargetSelectorGui());
            else 
            {
                //TODO display msg of unsuccess
            }
        }

        private Scene createTargetSelectorGui()
        {
            SelectTargetGui stgui = new SelectTargetGui(BattleManager.getInstance().turnExpert.m_Teams);
            stgui.isPopUp = true;
            stgui.position = new Vector2(0, 550);
            stgui.LoadContent();
            return stgui;
        }


        #region IOnCancel Members

        public void onCancel()
        {
            SceneManager.getInstance().markForRemove( this);
        }

        #endregion

        #region IOnElementPointed Members

        public void onElementPointed(Element e)
        {
            Panel descPanel = (Panel)basePanel.components[1];
            SpriteFont font = Imaginarium.ImaginariumGame.getInstance().Content.Load<SpriteFont>(CodeParameters.battleCommandFont);
            SkillDetail sd = (SkillDetail)e.associatedObject;

            descPanel.components.Clear();
            descPanel.components.Add(new Label(descPanel, new Text(font, sd.skill.description, Color.White, OriginType.LEFT_UP)));
            descPanel.components.Add(new Label(descPanel, new Text(font, "Cost: "+sd.calcMPCost(), Color.White, OriginType.LEFT_UP)));
            descPanel.components.Add(new Label(descPanel, new Text(font, "Level: "+sd.skillLevel, Color.White, OriginType.LEFT_UP)));
            descPanel.layout.refresh();
        }

        #endregion
    }
}
